# -*- coding: utf-8 -*-
# @Time : 2024/6/7 9:25 AM 
# @Author : Wayne
# @Email : 1
# @File : read_file_to_json_map.py
# @Project : young_python_demo
# @DESCRIPTION :

import json


def read_and_process_file(filename, keys_to_keep):
    result = []

    # 读取文件
    with open(filename, 'r') as file:
        content = file.read()
        # 将内容解析为Python对象
        data = json.loads(content)
        data = data.get("rows")
        # 处理数据，只保留特定的键值对
        for item in data:
            filtered_item = {key: item[key] for key in keys_to_keep if key in item}
            result.append(filtered_item)

    return result


# 使用函数
filename = 'a.txt'  # 你的文件名
keys_to_keep = ["id", "itemCode", "itemName", "pjCode", "pjName", "upCode", "upName"]  # 你想要保留的键

new_data = read_and_process_file(filename, keys_to_keep)
# print(new_data)
table_name = "customer_midd_data.st_car_config_upd_goods"
insert_statements = []

for item in new_data:

    insert_statement = f"INSERT INTO customer_midd_data.st_car_config_upd_goods (id, itemCode, itemName, pjCode, pjName, upCode, upName) VALUES ({item['id']}, '{item['itemCode']}', '{item['itemName']}', '{item['pjCode']}', '{item['pjName']}', '{item['upCode']}', '{item['upName']}');"

    insert_statements.append(insert_statement)


# 输出所有的插入语句

for statement in insert_statements:

    print(statement)